<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>
        权限管理
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" th:href="@{/assets/admin/css/font.css}">
    <link rel="stylesheet" th:href="@{/assets/admin/css/xadmin.css}">
    <script th:src="@{/assets/admin/lib/layui/layui.js}" charset="utf-8"></script>
    <script type="text/javascript" th:src="@{/assets/admin/js/xadmin.js}"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style type="text/css">
        .layui-form-switch {
            margin-top: 8px;
        }
        .layui-tree-checked{
            background-color:#d5e8fc;
        }

        .site-title fieldset {
            border: none;
            padding: 0;
            border-top: 1px solid #eee;
        }

        fieldset {
            display: block;
            margin-inline-start: 2px;
            margin-inline-end: 2px;
            padding-block-start: 0.35em;
            padding-inline-start: 0.75em;
            padding-inline-end: 0.75em;
            padding-block-end: 0.625em;
            min-inline-size: min-content;
            border-width: 2px;
            border-style: groove;
            border-color: rgb(192, 192, 192);
            border-image: initial;
        }


    </style>
</head>
<body>
<div class="x-nav">
           <span class="layui-breadcrumb">
              <a><cite>首页</cite></a>
              <a><cite>系统设置</cite></a>
              <a><cite>权限管理</cite></a>
            </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i></a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md3">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div id="permission_tree">
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-col-md9">
            <div class="layui-card">
                <div class="layui-card-body">
                    <form class="layui-form" lay-filter="menu-form">
                        <input type="hidden" name="id" value="" />
                        <div class="layui-form-item">
                            <label for="is_root_permission" class="layui-form-label">
                                <span class="x-red">*</span>是否根权限
                            </label>
                            <div class="layui-input-inline">
                                <input type="checkbox" name="is_root_permission" id="is_root_permission"
                                       title="根标识" lay-filter="isRoot" />
                            </div>
                        </div>
                        <div class="layui-form-item" id="parentIdDiv">
                            <label for="parentId" class="layui-form-label">
                                <span class="x-red">*</span>父权限
                            </label>
                            <div class="layui-input-inline">
                                <select id="parentId" name="parentId">
                                    <option value="">请选择所在父权限</option>
                                    <option th:each="per:${oneLevelPerList}" th:value="${per.id}"
                                            th:text="${per.permissionName}"></option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label for="permissionName" class="layui-form-label">
                                <span class="x-red">*</span>权限名
                            </label>
                            <div class="layui-input-block">
                                <input type="text" id="permissionName" name="permissionName" required="" lay-verify="required"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label for="permissionCode" class="layui-form-label">
                                <span class="x-red">*</span>权限编号
                            </label>
                            <div class="layui-input-inline">
                                <input type="text" id="permissionCode" name="permissionCode" required="" lay-verify="required"
                                       autocomplete="off" class="layui-input">
                            </div>
                            <div class="layui-form-mid layui-word-aux">
                                <span class="x-red">*</span>唯一标识
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <button class="layui-btn" name="addBtn" lay-filter="add" lay-submit="">
                                增加
                            </button>
                            <button class="layui-btn" name="updateBtn" lay-filter="update" lay-submit="">
                                更新
                            </button>
                            <button class="layui-btn" name="resetBtn" >
                                清空
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="layui-container">
</div>
</body>
<script th:src="@{/assets/admin/js/jquery.min.js}"></script>
<script th:replace="systemCommon/HarinCommon :: commonJs"></script>
<script>
    let currentData= null;

    $("input[name='resetBtn']").click(function(){
        form.reset();
        $("#parentName").text("");
    });

    layui.use(["laydate", "form", "layer", "tree","jquery"], function () {
        $ = layui.jquery;//jquery
        laydate = layui.laydate;//日期插件
        layer = layui.layer;//弹出层
        tree = layui.tree;
        form = layui.form;

        tree_obj = {
            elem: '#permission_tree',
            data: null,
            id: 'id',
            showCheckbox: false,      //是否显示复选框
            onlyIconControl: true,    //有单击事件，所以这个应该关闭
            showLine: true,           //是否开启连接线。默认 true，若设为 false，则节点左侧出现三角图标。
            edit: ['del'],      //是否开启节点的操作图标
            accordion: true,
            text: {
                defaultNodeName: '未命名',     //节点默认名称
                none: '无数据'                 //数据为空时的提示文本
            },
            click: function (obj) {             //单击事件
                $.ajax({
                    url: "/permission/getPermissionById",
                    type: "get",
                    data: {"id": obj.data.id},
                    dataType: "JSON",
                    success: function (result) {
                        var data = result.result;
                        currentData = data;
                        form.val("menu-form", {
                            "id": data.id,
                            "permissionName": data.permissionName,
                            "permissionCode":data.permissionCode,
                            "parentId": data.parentId
                        });

                        //$("#parentId option[value='"+data.parentId+"']").attr("selected",'true');

                        $("#is_root_permission").prop("checked", data.level == 1);
                        if(data.level == 1){ $('#parentIdDiv').hide();}else{ $('#parentIdDiv').show();}
                        form.render();
                    }

                });

            },
            operate: function (obj) {       //操作
                var type = obj.type; //得到操作类型：add、edit、del
                var data = obj.data; //得到当前节点的数据
                var elem = obj.elem; //得到当前节点元素

                //Ajax 操作
                var id = data.id; //得到节点索引
                if (type === 'add') { //增加节点
                    console.log(data);
                    // if( data.level != 1 ){
                    //     layer.msg("只允许给根权限添加子权限！");
                    //     return false;
                    // }
                    // $("#parentName").text(data.title);
                    // $("#parentId").val(data.id);
                    // $("#is_root_permission").prop("checked", false);
                    // $("#permissionName").val("");
                    // $("#permissionCode").val("");
                    // form.render();
                    // return false;
                } else if (type === 'update') { //修改节点
                    //console.log(elem.find('.layui-tree-txt').html()); //得到修改后的内容
                } else if (type === 'del') { //删除节点
                    //console.log(id + "被删除了")
                    console.log(type);
                    console.log(data);
                    console.log(elem);
                    $.ajax({
                        url: "/permission/deletePermissionById",
                        type:"delete",
                        data:{"id":data.id},
                        success:function(data){
                            if (data.code == 200) {
                                layer.alert(data.message, {icon: 6}, function (index) {
                                    layer.close(index);
                                });
                            } else {
                                layer.alert(data.message, {icon: 5}, function (index) {
                                    layer.close(index);
                                });
                            }
                        }
                    })
                };
            }
        }

        permissionTree = tree.render(tree_obj);

        getData();

        form.on('checkbox(isRoot)', function(data){
            if(data.elem.checked==true){
                $('#parentIdDiv').hide();
            }else{
                $('#parentIdDiv').show();
            }

        });

        //监听提交
        form.on('submit(add)', function (data) {
            let field = data.field;
            var isRoot = field.is_root_permission;
            if(isRoot == 'on'){
                field.parentId = '';
            }
            //发异步，把数据提交给后台处理
            $.ajax({
                url: "/permission/addPermission",
                data: JSON.stringify(field),
                type: "post",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                success: function (data) {
                    //console.log(data.message);
                    if (data.code == 200) {
                        layer.alert(data.message, {icon: 6}, function (index) {
                            // 获得frame索引
                            //var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            layer.close(index);
                        });
                        getData();
                    } else {
                        layer.alert(data.message, {icon: 5}, function (index) {
                            // 获得frame索引
                            //var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            layer.close(index);
                        });
                    }
                }
            });
            return false;
        });

        form.on('submit(update)', function (data) {
            //console.log(typeof data.field);
            var status = data.field.status;
            data.field.id = new Number(data.field.id);
            if ("on" == status) {
                data.field.status = 1;
            } else {
                data.field.status = 0;
            }
            if(isEmpty(data.field.parentId)){
                data.field.level = 1;
                data.field.parentId = 0;
            }else{
                data.field.level = 0;
            }
            //console.log(data);
            //发异步，把数据提交给后台处理
            $.ajax({
                url: "/permission/updatePermissionById",
                data: JSON.stringify(data.field),
                type: "post",
                dataType: "json",
                contentType: "application/json;charset=utf-8",
                success: function (data) {
                    //console.log(data.message);
                    if (data.code == 200) {
                        layer.alert(data.message, {icon: 6}, function (index) {
                            // 获得frame索引
                            //var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            layer.close(index);
                        });
                        getData();
                    } else {
                        layer.alert(data.message, {icon: 5}, function (index) {
                            // 获得frame索引
                            //var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            layer.close(index);
                        });
                    }
                }
            });
            return false;
        });

        $("input[name='id']").on('change',function(event){
            //console.log(123);
            $("button[name='addBtn']").hide();
            $("button[name='updateBtn']").show();
        });
    });


    /*获取后台的菜单数据*/
    function getData() {
        //layui.use(['tree'], function () {
        //tree = layui.tree;
        $.ajax({
            url: "/permission/getPermissionLayuiTree",    //后台数据请求地址
            type: "get",
            async: false,
            success: function (resut) {
                res_data = resut.result;
                tree_obj.data = res_data;
                permissionTree.reload(tree_obj);
            }
        });
        //});
    }
</script>
</html>